// pages/refund/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resData: '',
    showTips: false,
    chatList: [],
    showMore: '',
    id: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    this.data.id = options.id;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.init();
  },
  init() {
    let that = this
    util.http({
      url: 'api/miniwechat/order/record',
      data: {
        order_id: this.data.id
      }
    }).then(res => {
      let chatArr = [],
        mark;
      switch (res.status) {
        case 1:
          res.state = '协商中';
          res.time = res.create_time;
          break
        case 2:
          res.state = '平台介入'
          res.time = res.create_time;
          break
        case 3:
          res.state = '退款成功';
          res.time = res.final_date;
          break
        case 4:
          res.state = '撤销退款'
          res.time = res.revoke_date;
          break
      }
      if (res.chat_list) {
        res.chat_list.reverse();
        mark = res.chat_list.length > 3 ? true : false
        res.chat_list.forEach(item => {
          if (item.money) {
            item.money = Number(item.money).toFixed(2);
          }
        })
      }
      // 退款订单数据
      console.log(res)
      that.setData({
        resData: res,
        chatList: chatArr,
        showMore: mark
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },
  viewMore() {
    this.setData({
      showMore: false
    })
  },

  //toXieShang
  toXieShang() {
    wx.navigateTo({
      url: '/pages/continue/index?userId=' + this.data.resData.user_id + '&recordId=' + this.data.resData.id + '&price=' + this.data.resData.refund_money,
    })
  },

  // 点击同意出现弹窗
  isShowTips() {
    console.log('触发了')
    this.setData({
      showTips: true
    })
  },

  closeShowTips() {
    this.setData({
      showTips: false
    })
  },
  platform() {
    let that = this;
    wx.showModal({
      content: '是否申请平台介入？',
      success(res) {
        if (res.confirm) {
          util.http({
            url: 'api/miniwechat/order/platformq',
            method: 'POST',
            data: {
              user_id: that.data.resData.user_id,
              record_id: that.data.resData.id,
            }
          }).then(res => {
            wx.showToast({
              title: '发送成功',
              icon: 'none',
              duration: 1000
            })
            that.init();
          }).catch(err => {
            wx.showToast({
              title: String(err),
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  Revoke() {
    let that = this;
    wx.showModal({
      content: '是否确定撤销退款申请？',
      success(res) {
        if (res.confirm) {
          util.http({
            url: 'api/miniwechat/order/revoke',
            method: 'POST',
            data: {
              user_id: that.data.resData.user_id,
              record_id: that.data.resData.id,
            }
          }).then(res => {
            wx.showToast({
              title: '撤销成功',
              icon: 'none',
              duration: 1000
            })
            that.init();
          }).catch(err => {
            wx.showToast({
              title: String(err),
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  agree() {
    let that = this;
    util.http({
      url: 'api/miniwechat/order/agreeq',
      method: 'POST',
      data: {
        user_id: that.data.resData.user_id,
        record_id: that.data.resData.id,
      }
    }).then(res => {
      wx.showToast({
        title: '发送成功',
        icon: 'none',
        duration: 1000
      })
      this.setData({
        showTips: false
      })
      that.init();
    }).catch(err => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
      this.setData({
        showTips: false
      })
    })

  },
})